﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace CrushGame
{
    public class CollisionObject2D
    {
        IBasicInterface _owner;

        Vector2 _location;
        Vector2 _size;

        public Vector2 Location
        {
            get { return _location; }
        }

        public Vector2 Size
        {
            get { return _size; }
        }

        public float Width
        {
            get { return _size.X; }
        }

        public float Height
        {
            get { return _size.Y; }
        }

        //
        public CollisionObject2D(IBasicInterface obj)
        {
            _owner = obj;
            _location = obj.Location;
            _size.X = obj.Sprite.Width;
            _size.Y = obj.Sprite.Height;
        }

        public void Update()
        {
            _location = _owner.Location;
            _size.X = _owner.Sprite.Width;
            _size.Y = _owner.Sprite.Height;
        }

        public bool CollidesWith(CollisionObject2D other, ref Vector2 collisionZoneLocation, ref Vector2 collisionZoneSize)
        {
            // Calculate the intersection zone, assuming this collision object is to the upper left of the other
            Vector2 intersectUpperLeft = other.Location;
            Vector2 intersectLowerRight = _location + _size;
            Vector2 intersectSize = intersectLowerRight - intersectUpperLeft;

            if (intersectSize.X > 0.0f || intersectSize.Y > 0.0f)
            {
                collisionZoneLocation = intersectUpperLeft;
                collisionZoneSize = intersectLowerRight - collisionZoneLocation;
                return true;
            }

            // Perform the same calculations but assume this collision object is to the lower right instead
            intersectUpperLeft = _location;
            intersectLowerRight = other.Location + other.Size;
            intersectSize = intersectLowerRight - intersectUpperLeft;

            if (intersectSize.X > 0.0f || intersectSize.Y > 0.0f)
            {
                collisionZoneLocation = intersectUpperLeft;
                collisionZoneSize = intersectLowerRight - collisionZoneLocation;
                return true;
            }
            
            // If both of the above failed, there is no intersection
            collisionZoneLocation = new Vector2();
            collisionZoneSize = new Vector2();
            return false;
        }

        public void Push(CollisionObject2D other)
        {
        }
    }
}
